package com.sls.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.page.Pagination;
import com.sls.sys.constant.ExceptionCodeDescription;
import com.sls.sys.dao.OperatorDAO;
import com.sls.sys.dao.RoleInfoDAO;
import com.sls.sys.pojo.OperatorInfoVO;
import com.sls.sys.pojo.RoleInfo;
import com.sls.sys.querybean.RoleInfoQuery;
import com.sls.sys.service.RoleInfoService;

@Service("roleInfoService")
public class RoleInfoServiceImpl implements RoleInfoService {
	@Resource(name = "roleInfoDAO")
	private RoleInfoDAO reInfoDAO;
	@Resource(name = "operatorDAO")
	private OperatorDAO operatorDAO;
	private Gson gson = CommonUtil.gson();

	@Override
	public void save(String body, Head head) throws Exception {
		RoleInfo roleInfo = gson.fromJson(body, RoleInfo.class);
		if (roleInfo==null || roleInfo.getName()==null || roleInfo.getRemark()==null) {
			throw new ServerDisponseException(
					ExceptionCodeDescription.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDescription.PARSE_REQUEST_ERROR
							.getDescription());
		}
		OperatorInfoVO o = operatorDAO.queryAllById(head.getUserId());
		roleInfo.setUpdateAccount(o.getAccount());
		roleInfo.setUpdateDate(new Date());
		roleInfo.setDeleted(false);
		roleInfo.setId(UUID.randomUUID().toString());
		reInfoDAO.insert(roleInfo);
	}

	@Override
	public void delete(String body, Head head) throws Exception {
		RoleInfo roleInfo = gson.fromJson(body, RoleInfo.class);
		if (roleInfo==null||roleInfo.getId()==null) {
			throw new ServerDisponseException(
					ExceptionCodeDescription.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDescription.PARSE_REQUEST_ERROR
							.getDescription());
		}
		roleInfo.setUpdateAccount(head.getUserId());
		reInfoDAO.deleteById(roleInfo.getId());
	}

	@Override
	public void updateById(String body, Head head) throws Exception {
		RoleInfo roleInfo = gson.fromJson(body, RoleInfo.class);
		validate(roleInfo);
		roleInfo.setUpdateAccount(head.getUserId());
		reInfoDAO.updateById(roleInfo);
	}

	@Override
	public RoleInfo findById(String body, Head head) throws Exception {
		RoleInfo roleInfo = gson.fromJson(body, RoleInfo.class);
		RoleInfo role = reInfoDAO.findById(roleInfo.getId());
		return role;
	}

	@Override
	public Pagination<RoleInfo> findPage(String body, Head head) throws Exception {
		RoleInfoQuery query = gson.fromJson(body, RoleInfoQuery.class);
		if (query==null) {
			throw new ServerDisponseException(
					ExceptionCodeDescription.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDescription.PARSE_REQUEST_ERROR
							.getDescription());
		}
		Pagination<RoleInfo> list = reInfoDAO.queryPage(query);
		return list;
	}

	private void validate(Object obj) throws ServerDisponseException {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDescription.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDescription.PARSE_REQUEST_ERROR
							.getDescription());
		}
	}

	@Override
	public List<RoleInfo> findAll(String body, Head head) throws Exception {
		List<RoleInfo> list = reInfoDAO.queryAll();
		return list;
	}

}
